<?php

defined('IN_XLP') or exit('Access Denied');
/*
 * 支付通用模型
 */

class payModel extends Model {

    //支付模块对应表
    static public $payModel = array(
        'steward' => 1, //小管家
        'ticket' => 2, //商家电子券
    );

    function __construct() {
        parent::__construct();
        $this->dbTable = 'pay';
    }

    /*
     * 根据id获取支付数据
     */

    public function getPayById($id, $field = '*') {
        // $rs = M('order')->field($field)->where(array('id' => $id))->find();
        $rs = D('order')->info($id , 'order_sn');
        if (empty($rs)) {
            return null;
        } else if ($field == '*' || strpos($field, ',') !== false) {   //多个字段返回数组
            return $rs;
        } else {  //单字段直接返回结果
            $field = trim($field, '`');
            return isset($rs[$field]) ? $rs[$field] : null;
        }
    }

    /*
     * 根据外部交易号获取支付数据
     */

    public function getPayBySN($out_sn, $payModel = '', $field = '*') {
        $rs = $this->field($field)->where(array('out_trade_no' => $out_sn, 'pay_model' => self::$payModel[$payModel]))->find();
        if (empty($rs)) {
            return null;
        } else if ($field == '*' || strpos($field, ',') !== false) {   //多个字段返回数组
            return $rs;
        } else { //单字段直接返回结果
            $field = trim($field, '`');
            return isset($rs[$field]) ? $rs[$field] : null;
        }
    }

    /*
     * 设置支付参数
     */

    public function setPayParams($data) {
        $payModel = self::$payModel[$data['pay_model']];
        $temp = array();
        $fieldList = $this->getTableFields();
        foreach ($fieldList['fields'] as $key => $val) {
            if (isset($data[$key])) {
                $temp[$key] = $data[$key];
                unset($data[$key]);
            } else {
                $temp[$key] = $val['value'];
            }
        }
        $temp['pay_model'] = $payModel;
        $temp['create_time'] = TIME;
        //如果尚有其它字段则均放入others
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k] = urlencode($v);
            }
            $temp['others'] = urldecode(json_encode($data));
        }
        return $this->insert($temp);
    }

    /*
     * 校验并获取支付信息
     */

    public function getPayParams($pay_id, $field = '*') {
        $pay_info = $this->getPayById($pay_id, $field);
        if ($pay_info) {
            return $pay_info;
        } else {
            return -1;  //支付参数错误
        }
    }

    /*
     * 生成微信支付参数
     */

    public function genWeixinPayParams($pay_id, $openid) {
        $pay_info = $this->getPayParams($pay_id);
        
        if (is_array($pay_info)) {
            //设置支付方式
            // M('pay')->update(array('pay_type' => 'wx'), array('id' => $pay_id));
            $setting = F('weixin/setting');
            $arr = array(
                'body'         => '资信借服务费支付',    //商品描述
                'total_fee'    => $setting['basic_setting']['soc_min_quota'], //交易服务费
                'out_trade_no' => $pay_info['order_sn'], //外部交易号
                'openid'       => $openid,
            );
            // $others = json_decode($pay_info['others'], true);   //其它参数
            // if(is_array($others)) {
            //     $arr = $arr + $others;
            // }
            
            //生成支付参数数组
            $weixin_params = D('weixinUnipay')->callWebPay($arr, U('uninotify/index'));
            return $weixin_params;
        } else {
            return $pay_info;
        }
    }



    public function genWeixinCreditPayParams($pay_id, $openid) {
        $pay_info = M('credit_order')->where(array('id'=>$pay_id))->find();
        
        if (is_array($pay_info)) {
            //设置支付方式
            $arr = array(
                'body'         => '资信借认证服务费支付',    //商品描述
                'total_fee'    => $pay_info['amount'], //交易服务费
                'out_trade_no' => $pay_info['pay_sn'], //外部交易号
                'openid'       => $openid,
            );
            
            
            //生成支付参数数组
            $weixin_params = D('weixinUnipay')->callWebPay($arr, U('uninotify/credit'));
            return $weixin_params;
        } else {
            return $pay_info;
        }
    }



    /*
     * 生成支付链接
     * @param $orderSn string 订单号
     * @param $payModel string 支付模块
     * @param $redirect string 支付完成回调地址
     * @return string 生成的支付链接
     */

    public function setPayUrl($orderSn, $payModel, $redirect) {
        return BASE_URL . 'pay/' . '?g=wxpay&m=index&showwxpaytitle=1&id=' . $this->getPayBySN($orderSn, $payModel, 'id') . '&redirect=' . base64_encode($redirect);
    }

    /*
     * 生成代付链接
     */

    public function setHelperUrl($orderSn, $payModel) {
        return U('wxpay/helper/index', array('id' => $this->getPayBySN($orderSn, $payModel, 'id')));
    }

}
